package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.enums.model.OrderStatus;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.order.model.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: atguigu
 * @create: 2023-05-09 15:53
 */
@Controller
public class PaymentController {


    @Autowired
    private OrderFeignClient orderFeignClient;

    /**
     * 渲染选择支付页面
     *
     * @param orderId
     * @return
     */
    @GetMapping("/pay.html")
    public String payChooseHtml(Model model, @RequestParam("orderId") Long orderId) {
        //1.远程调用获取订单信息
        OrderInfo orderInfo = orderFeignClient.getOrderInfoById(orderId);
        //2.判断订单状态 给静态页赋值
        if (orderInfo != null && OrderStatus.UNPAID.name().equals(orderInfo.getOrderStatus())) {
            model.addAttribute("orderInfo", orderInfo);
            return "/payment/pay";
        }
        throw new RuntimeException("订单状态异常!");
    }

    /**
     * 用户支付成功后,展示支付成功页面
     *
     * @return
     */
    @GetMapping("/pay/success.html")
    public String paySuccessHtml() {
        return "/payment/success";
    }
}

